home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / removefile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-06  |  4.4 KB  |  176 lines

  1. /*
  2.  
  3.     RemoveFile XCMD v1.2
  4.     
  5.     ©1990-1 Apple Computer, Inc.; by Mike Byrne
  6.     
  7.     RemoveFile takes a full pathname and deletes the file.  It can also be "fooled" into deleting a folder
  8.     if the pathname to the folder is passed without a colon at the end. The folder must be empty for this
  9.     to work.
  10.     
  11.     Form:
  12.     RemoveFile <full pathname>
  13.     
  14.     # the MPW 3.2 build commands:
  15.     C -b RemoveFile.c -mbg off
  16.         Link -w -t STAK -c WILD -rt XCMD=603 ∂
  17.             -m ENTRYPOINT ∂
  18.             -sg RemoveFile ∂
  19.             RemoveFile.c.o ∂
  20.             "{Libraries}HyperXLib.o" ∂
  21.             "{Libraries}Runtime.o" ∂
  22.             "{Libraries}Interface.o" ∂
  23.             "{CLibraries}StdCLib.o" ∂
  24.             -o "some stack"
  25. */
  26.  
  27. #include <Types.h>
  28. #include <Packages.h>
  29. #include <string.h>
  30. #include <Memory.h>
  31. #include <Files.h>
  32. #include <Errors.h>
  33. #include "HyperXCmd.h"
  34.  
  35. #define NULL 0L
  36. #define NIL OL
  37.  
  38. #define kNumParams 1            
  39.  
  40.  
  41. /* prototypes */
  42. void ErrorBack(XCmdPtr paramPtr, char *message);
  43. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  44. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  45.  
  46.  
  47. pascal void EntryPoint(XCmdPtr paramPtr)
  48. {
  49.     short         i,j;
  50.     char        volName[34];
  51.     char        ppathName[301];
  52.     short        vRefNum;
  53.  
  54.  
  55.     /* move high and lock the parameters. */
  56.     MoveLockParams(paramPtr, paramPtr->paramCount);
  57.  
  58.     /* check for copyright or syntax help request */
  59.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  60.         ErrorBack(paramPtr, "v1.2, ©1990-1 Apple Computer, Inc.; by Mike Byrne");
  61.         UnlockParams(paramPtr, paramPtr->paramCount);
  62.         return;
  63.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  64.         ErrorBack(paramPtr, "RemoveFile syntax is 'RemoveFile(<full pathname>'");
  65.         UnlockParams(paramPtr, paramPtr->paramCount);
  66.         return;
  67.     }
  68.  
  69.     /* not a copyright or help request.       */     
  70.     /* check for correct number of parameters */
  71.     if (paramPtr->paramCount != kNumParams) {
  72.         ErrorBack(paramPtr, "Error: RemoveFile syntax is 'RemoveFile(<full pathname>'");
  73.         UnlockParams(paramPtr, paramPtr->paramCount);
  74.         return;
  75.     }
  76.  
  77.  
  78.     /*  extract the volume name from the handle, copy to a pas string,
  79.          and get the volume reference number of the volume              */
  80.     for (i=0; ((*(paramPtr->params[0]))[i] != ':' && (i < 33)); i++) 
  81.         { volName[i] = (*(paramPtr->params[0]))[i]; }
  82.     volName[i] = ':';
  83.     volName[i+1] = '\0'; 
  84.     c2pstr(volName);
  85.     vRefNum = 0;
  86.     
  87.     if (SetVol(volName, vRefNum) != noErr) {                        // toolbox
  88.         ErrorBack(paramPtr, "Error: Could not set the default volume");
  89.         UnlockParams(paramPtr, kNumParams);
  90.         return;
  91.     }
  92.     
  93.     if (GetVol(&volName, &vRefNum) != noErr) {                        // toolbox
  94.         ErrorBack(paramPtr, "Error: Could not find the volume requested.");
  95.         UnlockParams(paramPtr, kNumParams);
  96.         return;
  97.     }
  98.  
  99.     /* now, copy the rest of the pathname to the partial pathname and convert it. */
  100.     for (j=i; (j <= strlen((*(paramPtr->params[0]))) && (j < 300)); j++) 
  101.         { ppathName[j-i] = (*(paramPtr->params[0]))[j]; }
  102.     c2pstr(ppathName);
  103.         
  104.     /* delete the file  */
  105.     switch (FSDelete(ppathName, vRefNum)) {                         // toolbox
  106.         case noErr:
  107.             return;
  108.             break;
  109.         case fBsyErr:
  110.             ErrorBack(paramPtr, "Error: Cannot delete file:  the file is busy.");
  111.             UnlockParams(paramPtr, kNumParams);
  112.             return; 
  113.             break;
  114.         case fLckdErr, vLckdErr, wPrErr:
  115.             ErrorBack(paramPtr, "Error: Cannot delete file:  the file or volume is locked.");
  116.             UnlockParams(paramPtr, kNumParams);
  117.             return; 
  118.             break;
  119.         case bdNamErr, fnfErr:
  120.             ErrorBack(paramPtr, "Error: Cannot delete file:  bad or non-existant file name.");
  121.             UnlockParams(paramPtr, kNumParams);
  122.             return;
  123.             break;
  124.     }
  125.     
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.     
  133. /* allocate and load up paramPtr->returnValue with a string 
  134.    -------------------------------------------------------- */
  135. void ErrorBack(XCmdPtr paramPtr, char *message)
  136. {
  137.     Handle  mesHnd;
  138.  
  139.     /*
  140.         Allocate space for an error message.
  141.         Copy the string into it.
  142.         Return the handle to HyperCard.
  143.     */
  144.     mesHnd = NewHandle((long)(strlen(message)+1));
  145.     if (mesHnd == nil) return;
  146.     strcpy((char *)*mesHnd,message);
  147.     paramPtr->returnValue = mesHnd;
  148. }
  149.  
  150.  
  151.  
  152. /*  move high and lock down all parameters (watch that these are read-only) 
  153.     ----------------------------------------------------------------------- */
  154. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  155. {
  156.     short i;
  157.     
  158.     for(i=0; i <= paramCount-1; i++)
  159.     {
  160.         MoveHHi(paramPtr->params[i]);
  161.         HLock(paramPtr->params[i]);
  162.     }
  163. }
  164.  
  165.  
  166.  
  167.  
  168. /* unlock all parameter handles in the XCmdBlock  
  169.    ---------------------------------------------  */
  170. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  171. {    short i;
  172.     
  173.     for(i=0; i <= paramCount-1; i++)
  174.         { HUnlock(paramPtr->params[i]);}
  175. }
  176.